$(document).ready(require);
$(document).ready(init);
function require() {
	var radios = jQuery("input[type='radio']");
	var allElems = document.getElementsByTagName('input');
	for (i = 0; i < allElems.length; i++) {
		if (allElems[i].type == 'radio' && allElems[i].value == '0') {
			allElems[i].checked = true;
		}
	}
	radios.on('click', function() {
		if ($(this).val() > 0) {
			$(this).closest('td').last('td').find('input:text').attr(
					'required', '');
		} else {
			$(this).closest("tr").find("td:nth-child(5)").find('input:text')
					.removeAttr('required');
		}

	});

	// $("#0").addClass("info");
	// $("#0").children($("#tdDrop")).find("#buttDrop").text("Dar de Alta");
	// ("#0").children($("#tdDrop")).find("#buttDrop").attr('class', 'button
	// success');
	elements = $(".0");// busca los inactivos
	elements.each(function() {
		$(this).addClass("info");
		$(this).children($("#tdDrop")).find("#buttDrop").text("Dar de Alta");
		$(this).children($("#tdDrop")).find("#buttDrop").attr('class',
				'button success')
	});

}
function init() {
	$('[data-change]').unbind();
	$('[data-drop]').unbind();
	$('[data-add]').unbind();
	$('[data-item]').unbind();
	$('[data-info]').unbind();
	$('[data-status]').unbind();
	$('[data-change]').click(changeCat);
	$('[data-drop]').click(activeCat);
	$('[data-add]').click(addCat);
	$('[data-item]').click(item);
	$('[data-info]').click(info);
	$('[data-status]').click(status);

}
function status(){
	var nameCat = ($(this).closest('tr').find('td:first').text()).toString();
	location.href = "AdminStatus.jsp?catName="+nameCat;
}
function info() {
	var fid = $(this).attr('class');
	location.href = "FormInfo.jsp?form=" + fid;
}
function item() {
	var nameCat = ($(this).closest('tr').find('td:first').text()).toString();
	location.href = "AdminItem.jsp?name=" + nameCat;
}
function validate(e) {
	(e.preventDefault) ? e.preventDefault() : e.returnValue = false;// stop the
	// form from
	// submitting
	// before
	// validating
	var div = document.getElementById('loading');

	window.location = '#modal3';
	document.getElementsByTagName('form')[0].submit();
}
function changeCat() {
	var old = ($(this).closest('tr').find('td:first').text()).toString();
	var nameCat = prompt("Ingrese el nuevo nombre", old);

	if (nameCat != null) {
		$(this).closest('tr').find('td:first').text(nameCat);
		var nuevo = $(this).closest('tr').find('td:first').text().toString();
		var cadena = "old=" + old + "&new=" + nuevo;
		$.ajax({
			type : "POST",
			url : "ChangeCat.jsp",
			data : cadena,
			success : function(dato) {
				alert("Ok!");
			}
		});
	}
}
function activeCat() {

	var nam = $(this).closest('tr').find('td:first').text().toString();

	var state;
	var a = $(this).parent().parent().attr('class');
	if (a == 1) {
		state = 0;

	} else {
		state = 1;
	}
	$(this).parent().parent().attr('class', state);
	var caden = "nam=" + nam + "&state=" + state;
	$.ajax({
		type : "POST",
		url : "DropCat.jsp",
		data : caden,
		success : function(dato) {
			alert(dato);

		}
	});
	if (state == 0) {
		$(this).parent().parent().addClass('info');
		$(this).text("Dar de Alta");
		$(this).attr('class', 'button success');
	} else {
		$(this).parent().parent().removeClass('info');
		$(this).text("Dar de Baja");
		$(this).attr('class', 'button warning');
	}
}
function addCat() {
	var nameCat = prompt("Ingrese el nombre", "");
	if (nameCat == null) {
		return; // break out of the function early
	}
	var toSend = "name=" + nameCat;
	$
			.ajax({
				type : "POST",
				url : "AddCat.jsp",
				data : toSend,
				success : function(dato) {
					alert(dato);
					if (dato.contains("Ok!")) {
						$("#table tbody")
								.prepend(
										"<tr class='1'><td>"
												+ nameCat
												+ "</td><td id = 'tdDrop'><button id='buttDrop' class='button warning' data-drop=''>Dar de Baja</button>"
												+ "</td><td id = 'tdChange'><button class='button default' data-change=''>Cambiar Nombre</button></td>"
												+ "<td id = 'tdChange'><button class='button default' data-item=''>Items</button></td>"
												+ "<td id = 'tdChange'><button class='button default' data-status=''>Estados</button></td></tr>");
						init();
					}
				}
			});

}
